$(function () {
    $(".head").load("head.html")
    $(".s_btn").click(function () {
        var c=$(".s_ipt").val()
        var context=escape(c)
        location.href="http://localhost:7777/sp.html?context="+context;
    })

    //拿地址后面的参数
    function getData(){
        if(document.URL.indexOf("?")<0) return;//获取当前url地址,如果没参数.直接返回
        var str=document.URL.split("?")[1];
        var obj={};
        var arr;
        //当只有一个参数传过来时
        if(str.indexOf("&")<0){
            if(str.indexOf("=")<0) return;
            arr=str.split("=");
            obj[arr[0]]=arr[1];
            return obj;
        }
        //当有多个参数时
        arr=str.split("&");
        var arr1;
        for(var i=0;i<arr.length;i++){
            arr1=arr[i].split("=");
            obj[arr1[0]]=arr1[1];
        }
        return obj;
    }

    var obj=getData();

    // var value = Escape.unescape(obj.fl);
    // console.log(unescape(obj.fl))
    pageInfo(1,8);
    //点击一级菜单或三级菜单会查出下面所有的商品，并进行分页
    function pageInfo(pageNum,PageSize) {
        $.post("news/tdSP",{"id":obj.id,"pageNum":pageNum,"pageSize":PageSize},function (data) {
            $(".cate_list").empty()
            $(".pages").empty()
            $.each(data.list,function (index,news) {
                if (news.isdelete==0&&news.stock>0){
                    $(".cate_list").append("<li>\n" +
                        "<div class=\"img\"><a href=\"javascript:;\"><img data-fl='"+news.name+"' data-id='"+news.id+"' class='imgXQ' src=\"../../files/"+news.filename+"\" width=\"210\" height=\"185\" /></a></div>\n" +
                        "<div class=\"price\">\n" +
                        "<font>￥<span>"+news.price+"</span></font> &nbsp; 26R\n" +
                        "</div>\n" +
                        "<div class=\"name\"><a href=\"javascript:;\">"+news.name+"</a></div>\n" +
                        "<div class=\"carbg\">\n" +
                        "<a href=\"javascript:;\" class=\"ss\" data-id='"+news.id+"'>收藏</a>\n" +
                        "<a href=\"javascript:;\"  class=\"j_car\" data-id='"+news.id+"'>加入购物车</a>\n" +
                        "</div>\n" +
                        "</li>")
                }
            })
            $(".ss").unbind('click').click(function () {
                $.post("news/scSP",{"id":$(this).data('id')},function (data) {
                    confirm(data.msg)
                    shoucang()
                })
            })
            $(".j_car").unbind('click').click(function () {
                $.post("news/gwcSP",{"id":$(this).data('id')},function (data) {
                    confirm(data.msg)
                    gwc()
                })
            })
            if (data.hasPreviousPage==true){
                $(".pages").append(
                    "<a href=\"#\" class=\"sp_pre\">上一页</a>"
                )
            }
            $.each(data.navigatepageNums,function (i,nav) {
                if(pageNum==nav){
                    $(".pages").append(
                        "<a href='javascript:;' style='background:red;' data-pagenum='"+nav+"' class=\"p_pre\">"+nav+"</a>"
                    )
                }else {
                    $(".pages").append(
                        "<a href='javascript:;' data-pagenum='"+nav+"' class=\"p_pre\">"+nav+"</a>"
                    )
                }

            })
            if (data.hasNextPage==true){
                $(".pages").append(
                    "<a href=\"#\" class=\"xp_pre\">下一页</a>"
                )
            }
            $(".p_pre").unbind('click').click(function () {
                var pagenum= $(this).data('pagenum')
                pageInfo(pagenum,8);
            })
            $(".sp_pre").unbind('click').click(function () {
                pageInfo(pageNum-1,8);
            })
            $(".xp_pre").unbind('click').click(function () {
                var pagenum= $(this).data('pagenum')
                pageInfo(pageNum+1,8);
            })
            $(".imgXQ").unbind('click').click(function () {
                var id=$(this).data('id')
                var fll=$(this).data('fl')
                var fl =unescape(obj.fl)+">"+fll;
                var fen=escape(fl)
                location.href="http://localhost:7777/Product.html?id="+id;
            })
        })
    }

    shoucang();
    gwc();
    var lala=0;
    fenci(0);


//购物车

    function gwc() {
        var zong=0;
        var prices=0;
        $.post("news/gouwuche",{},function (data) {
            $(".cars").empty()
            console.log(data)
            $.each(data.list,function (index,news) {
                // var sid=news.id;
                var num=data.num;
                // zong+=data.num[sid]
                zong+=news.num
                prices+=(news.num)*(news.price);
                // prices+=(data.num[sid])*news.price
                $(".car_t").find("span").html(zong)
                $(".price_sum").find("span").html(prices)
                $(".car_bg").find(".cars").append(
                    "<li>\n" +
                    "<div class=\"img\"><a href=\"javascripr:;\"><img src='../../files/"+news.filename+"' width=\"58\" height=\"58\"/></a></div>\n" +
                    "<div class=\"name\"><a href=\"javascripr:;\">"+news.name+"</a></div>\n" +
                    "<div class=\"price\"><font color=\"#ff4e00\">￥"+news.price+"</font> X"+news.num+"</div>\n" +
                    "</li>"
                )
            })
            console.log(data)
            $(".GWjs").click(function () {
                if (prices>0){
                    location.href="http://localhost:7777/BuyCar.html";
                }
            })
        })
    }
    function shoucang() {
           $.post("news/shoucang",{},function (data) {
               $("#scUL").empty();
               console.log(data)
               if (data!=null){
                   $.each(data,function (index,news) {
                       console.log(news)
                       $("#scUL").append(
                           "<li>\n" +
                           "<div class=\"img\"><a href=\"javascript:;\"><img src=\"../../files/"+news.filename+"\" width=\"185\" height=\"162\" /></a></div>\n" +
                           "<div class=\"name\"><a href=\"javascript:;\">"+news.name+"</a></div>\n" +
                           "<div class=\"price\">\n" +
                           "<font>￥<span>"+news.price+"</span></font> &nbsp; 18R\n" +
                           "</div>\n" +
                           "</li>"
                       )
                   })
               }
           })
       }
       function fenci(pageNum) {

           var c=unescape(obj.context)
           $.post("news/fenci",{"c":c,"p":pageNum},function (data) {
               $(".cate_list").empty()
               $(".pages").empty()
               console.log(data)
               $.each(data.list,function (index,news) {
                   if (news.isdelete==0){
                       if (data.map[news.id].name_ik==undefined){
                           $(".cate_list").append("<li>\n" +
                               "<div class=\"img\"><a href=\"javascript:;\"><img data-fl='"+news.name+"' data-id='"+news.id+"' class='imgXQ' src=\"../../files/"+news.filename+"\" width=\"210\" height=\"185\" /></a></div>\n" +
                               "<div class=\"price\">\n" +
                               "<font>￥<span>"+news.price+"</span></font> &nbsp; 26R\n" +
                               "</div>\n" +
                               "<div class=\"name\"><a href=\"javascript:;\">"+news.name+"</a></div>\n" +
                               "<div class=\"carbg\">\n" +
                               "<a href=\"javascript:;\" class=\"ss\" data-id='"+news.id+"'>收藏</a>\n" +
                               "<a href=\"javascript:;\" class=\"j_car\" data-id='"+news.id+"'>加入购物车</a>\n" +
                               "</div>\n" +
                               "</li>")
                       }else {
                           $(".cate_list").append("<li>\n" +
                               "<div class=\"img\"><a href=\"javascript:;\"><img data-fl='"+news.name+"' data-id='"+news.id+"' class='imgXQ' src=\"../../files/"+news.filename+"\" width=\"210\" height=\"185\" /></a></div>\n" +
                               "<div class=\"price\">\n" +
                               "<font>￥<span>"+news.price+"</span></font> &nbsp; 26R\n" +
                               "</div>\n" +
                               "<div class=\"name\"><a href=\"javascript:;\">"+data.map[news.id].name_ik+"</a></div>\n" +
                               "<div class=\"carbg\">\n" +
                               "<a href=\"javascript:;\" class=\"ss\" data-id='"+news.id+"'>收藏</a>\n" +
                               "<a href=\"javascript:;\" class=\"j_car\" data-id='"+news.id+"'>加入购物车</a>\n" +
                               "</div>\n" +
                               "</li>")
                       }

                   }
               })
               $(".ss").unbind('click').click(function () {
                   $.post("news/scSP",{"id":$(this).data('id')},function (data) {
                       confirm(data.msg)
                       shoucang()
                   })
               })
               $(".j_car").unbind('click').click(function () {
                   $.post("news/gwcSP",{"id":$(this).data('id')},function (data) {
                       confirm(data.msg)
                       gwc()
                   })
               })
               $(".imgXQ").unbind('click').click(function () {
                   var id=$(this).data('id')
                   var fll=$(this).data('fl')
                   var fl =unescape(obj.fl)+">"+fll;
                   var fen=escape(fl)
                   location.href="http://localhost:7777/Product.html?id="+id;
               })
               if (lala>=1){
                   $(".pages").append(
                       "<a href=\"javascript:;\" class=\"sp_pre\">上一页</a>"
                   )
               }
               if (data.list.length==4){
                   $(".pages").append(
                       "<a href=\"javascript:;\" class=\"xp_pre\">下一页</a>"
                   )
               }
               $(".sp_pre").click(function () {
                   fenci((lala-1)*4)
                   lala-=1;
               })
               $(".xp_pre").click(function () {
                   fenci((lala+1)*4)
                   lala+=1;
               })

           })
       }

})